<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>Redis源码阅读02-数据结构sds - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="Redis源码阅读02-数据结构sds">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="Redis源码阅读02-数据结构sds - 天地维杰网" />
    <meta name="description" content="c语言的字符串表示有一下几个缺点： - c语言的字符串是以`�`作为判断数组结束的标记，所以如果存储的字符串中包含`�`的话，则无法获取全部的字符。 - c语言字符串的获取字符串长度，只能将内容进行遍历后才能获取到长度，时间复杂度是O(n)。 - c语言的字符串不记录长度，在拼接长度时可能造成内存溢出。" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis-read-code-02-about-sds/" itemprop="url">
        Redis源码阅读02-数据结构sds
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-11-08">
    2022-11-08
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/redis" itemprop="url" rel="index">
        <span itemprop="name">redis</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">原创</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">1941 字 ~4分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h2 id="redis为什么不使用c语言对字符串的实现">redis为什么不使用c语言对字符串的实现？</h2>

<p>c语言的字符串表示有一下几个缺点：
- c语言的字符串是以<code>\0</code>作为判断数组结束的标记，所以如果存储的字符串中包含<code>\0</code>的话，则无法获取全部的字符。
- c语言字符串的获取字符串长度，只能将内容进行遍历后才能获取到长度，时间复杂度是O(n)。
- c语言字符串不记录自身的长度，所以strcat方法已经认为用户在执行此函数时已经为dest分配了足够多的内存，足以容纳src字符串中的所有内容，而一旦这个条件不成立就会产生缓冲区溢出，会把其他数据覆盖掉。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20221109185413.png" width = 400 /> </div>

<p>sds解决了以上问题
- 拥有len属性记录字符串长度，获取长度的时间复杂度由O(n)变为直接获取len属性的O(1)
- sds在拼接时首先会获取可用空间，并进行判断是否需要进行扩容，所以不会造成缓存区溢出，这点上比c语言的string是更安全的
- sds在扩容时实现了空间预分配，申请内存时会多申请一部分内存出来，一定程度上避免频繁申请内存。</p>

<h2 id="空间预分配">空间预分配</h2>

<p>sds预空间分配的实现就在 sds.c的<code>sdsMakeRoomFor</code>方法中，首先通过<code>sdsavail</code>方法获取sds的剩余空间，然后判断剩余空间是否足够新增长度的使用，如果足够使用则直接返回，如果不足够使用，则判断（新增长度+已用长度）所需长度是否是小于1M，如果小于1M，则直接加倍。这种方式相当于预分配了一部分内存，</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000">sds</span> <span style="color:#000">sdsMakeRoomFor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">newsh</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//获取sds剩余空间
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">size_t</span> <span style="color:#000">avail</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsavail</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">size_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">reqlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">oldtype</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">size_t</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* Return ASAP if there is enough space left. */</span>
    <span style="color:#8f5902;font-style:italic">//判断字符串的剩剩余空间是否足够，如果足够直接返回
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">avail</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//获取字符串的原长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdslen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">oldtype</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//字符串的新长度 = 原长度 + 新增长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">reqlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">len</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">assert</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>   <span style="color:#8f5902;font-style:italic">/* Catch size_t overflow */</span>
    <span style="color:#8f5902;font-style:italic">//这里判断新长度是否超过了1M
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_MAX_PREALLOC</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#8f5902;font-style:italic">//没超过直接申请所需长度的2倍
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">else</span>
        <span style="color:#8f5902;font-style:italic">//超过了1M则申请newlen + 1M的长度
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_MAX_PREALLOC</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// 转换新长度的类型
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsReqType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* Don&#39;t use type 5: the user is appending to the string and type 5 is
</span><span style="color:#8f5902;font-style:italic">     * not able to remember empty space, so sdsMakeRoomFor() must be called
</span><span style="color:#8f5902;font-style:italic">     * at every appending operation. */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//根据新长度转换的类型获取hdr结构体的长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">assert</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">reqlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">/* Catch size_t overflow */</span>
    <span style="color:#8f5902;font-style:italic">//如果类型没有变更 
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">oldtype</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s_realloc_usable</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">newlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">usable</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">/* Since the header size changes, need to move the string forward,
</span><span style="color:#8f5902;font-style:italic">         * and can&#39;t use realloc */</span>
        <span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s_malloc_usable</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">newlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">usable</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">len</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">s_free</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">sdssetlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">sdsTypeMaxSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsTypeMaxSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">sdssetalloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<h2 id="sds的指针位置">sds的指针位置</h2>

<p>sds新建字符串的方法 sdsnew和sdsnewlen的实现方法，可以看到，s对应的指针，就是buf的所在位置，所以sds也可以直接当做string来使用，巧妙的指针使用也是让sds完美兼容string，另外因为sdshdr的定义是使用的紧凑式内存对齐（参考<a href="https://blog.csdn.net/wejack/article/details/127694676">sds对齐</a>），还可以通过buf的位置-1直接找到flags的位置。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000">sds</span> <span style="color:#000">_sdsnewlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">init</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">trymalloc</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//这里定义了sds的对象s
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//根据长度转换sds的type
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsReqType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">/* Empty strings are usually created in order to append. Use type 8
</span><span style="color:#8f5902;font-style:italic">     * since type 5 is not good at this. */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//根据 type计算出sds sdshdr的长度。
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//定义flags的指针
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* flags pointer. */</span>
    <span style="color:#000">size_t</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">assert</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* Catch size_t overflow */</span>
    <span style="color:#8f5902;font-style:italic">//申请内存
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">trymalloc</span><span style="color:#ce5c00;font-weight:bold">?</span>
        <span style="color:#000">s_trymalloc_usable</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">initlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">usable</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">:</span>
        <span style="color:#000">s_malloc_usable</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">initlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">usable</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">init</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">SDS_NOINIT</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">init</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">init</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">memset</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">initlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//将sds的指针指向 sh+hdrlen，也就是buf数组的位置
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//s-1的位置也就是 flags的位置
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">sdsTypeMaxSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">usable</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsTypeMaxSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_TYPE_BITS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">usable</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">init</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">init</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">\0</span><span style="color:#4e9a06">&#39;</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<h2 id="free-space-可用空间">free space 可用空间</h2>

<p>free space可用空间（也有文章翻译叫惰性空间），一方面是指空间预分配以后除去占用的空间以外空余的空间，另一方面，在sds进行修改时，并不会将sds已经申请的buf[]空间的内存进行回收，仅是将sds的len值进行重新设置，已经申请的空间在那里留着以后再用。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">sdssetlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_TYPE_BITS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20221109183604.png" width = 400 /> </div>

<h2 id="关于sds的最大长度为512mb">关于sds的最大长度为512mb</h2>

<p>当客户端操作 client 时，一般不会直接使用 sds ，而是通过对象的方式来使用。比如创建的字符串其实是一个对象，间接使用到了 sds 结构。限制 512M 的逻辑在 t_string.c 的 checkStringLength 方法。</p>

<p>在redis3.2.13、redis4.0.14、redis5.0.9版本里面的的一个方法，<code>checkStringLength</code>里面写死了限制<code>512*1024*1024</code>，这个方法常在 <code>SET/HSET</code>的时候被调用。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">checkStringLength</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">client</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">size</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#0000cf;font-weight:bold">512</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">addReplyError</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">string exceeds maximum allowed size (512MB)</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_ERR</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_OK</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>而在redis6.2.7版本里面，这个长度已经改为读取配置 <code>proto-max-bulk-len</code>的长度了。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">checkStringLength</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">client</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">CLIENT_MASTER</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">size</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">proto_max_bulk_len</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">addReplyError</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">string exceeds maximum allowed size (proto-max-bulk-len)</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_ERR</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_OK</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>参考文档：</p>

<p><a href="https://www.cnblogs.com/sulishihupan/p/14512066.html">https://www.cnblogs.com/sulishihupan/p/14512066.html</a></p>

<p><a href="https://blog.csdn.net/wejack/article/details/127250866">https://blog.csdn.net/wejack/article/details/127250866</a></p>

<p><a href="https://blog.csdn.net/wejack/article/details/127694676">https://blog.csdn.net/wejack/article/details/127694676</a></p>

<p><a href="https://juejin.cn/post/7064359936842530830">https://juejin.cn/post/7064359936842530830</a></p>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/redis" rel="tag" title="redis">#redis#</a>
    
    <a href="http://www.shutdown.cn/tags/code" rel="tag" title="code">#code#</a>
    
    <a href="http://www.shutdown.cn/tags/server" rel="tag" title="server">#server#</a>
    
    <a href="http://www.shutdown.cn/tags/%e6%ba%90%e7%a0%81" rel="tag" title="源码">#源码#</a>
    
    <a href="http://www.shutdown.cn/tags/sds" rel="tag" title="sds">#sds#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis-read-code-01-start-flow/" rel="next" title="Redis源码阅读01-读了一下redis启动流程的源码">
        <i class="fa fa-chevron-left"></i> Redis源码阅读01-读了一下redis启动流程的源码
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/why-redis-sds-max-length-limit-is-512mb/" rel="prev" title="为什么redis的SDS的最大长度限制为512mb">
        为什么redis的SDS的最大长度限制为512mb <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#redis为什么不使用c语言对字符串的实现">redis为什么不使用c语言对字符串的实现？</a></li>
<li><a href="#空间预分配">空间预分配</a></li>
<li><a href="#sds的指针位置">sds的指针位置</a></li>
<li><a href="#free-space-可用空间">free space 可用空间</a></li>
<li><a href="#关于sds的最大长度为512mb">关于sds的最大长度为512mb</a></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>